package thread;

import java.sql.SQLOutput;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolDemo1 {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 5; i++) {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread t = Thread.currentThread();
                        System.out.println("正在执行一个线程任务："+t.getName());
                        Thread.sleep(5000);
                        System.out.println("执行完毕");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            /*Thread t = new Thread(r);
            t.start();*/
            threadPool.execute(r);
            System.out.println("一个任务加入了线程池");
        }
        threadPool.shutdown();//该方法会等待正在执行的任务完成后关闭线程池

        /*threadPool.shutdownNow();*///立即停止线程池，并取消所有未完成的任务,
                                //      当一个线程调用sleep方法处于睡眠阻塞状态的过程中，
                                //      如果该线程的interrupt方法被调用，则sleep方法会立即抛出中断异常
        System.out.println("停止线程");
    }
}
